package com.xxx.dao;

import com.xxx.pojo.Student;
import com.xxx.utils.DBUtil;

import java.sql.*;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Iterator;

/**
 * @version v1.0
 * @Description:
 * @Author zhanzhongxin
 * @Date 2021/7/29 10:48
 * @Copyright 博为峰
 */
public class StudentDaoImpl implements StudentDao {
    @Override
    public int insert(ArrayList<Student> list) {
        String sql = "insert into stu(name,age,height,sex,birthday,cla_id) values (?, ?, ?, ?, ?, ?)";
        try (final Connection connection = DBUtil.getConnection();
             final PreparedStatement preparedStatement = connection.prepareStatement(sql);) {
            int i = 0;
            Iterator<Student> iterator = list.iterator();
            while (iterator.hasNext()) {
                Student student = iterator.next();
                preparedStatement.setString(1, student.getName());
                preparedStatement.setInt(2, student.getAge());
                preparedStatement.setDouble(3, student.getHeight());
                preparedStatement.setString(4, student.getSex());
                preparedStatement.setString(5, student.getBirthday());
                preparedStatement.setInt(6, student.getClaId());
                i = preparedStatement.executeUpdate();
            }
            return i;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }

    @Override
    public int delete(int studentId) {
        return 0;
    }

    @Override
    public int update(Student student) {
        return 0;
    }

    @Override
    public ArrayList<Student> query(Student student) {
        final ArrayList<Student> list = new ArrayList<>();
        try (final Connection connection = DBUtil.getConnection();
             final PreparedStatement preparedStatement = connection.prepareStatement("select * from stu");
             final ResultSet resultSet = preparedStatement.executeQuery();) {
            while (resultSet.next()) {
                final Student stu = new Student();
                stu.setId(resultSet.getInt("id"));
                stu.setName(resultSet.getString("name"));
                stu.setAge(resultSet.getInt("age"));
                stu.setHeight(resultSet.getDouble("height"));
                final Timestamp birthday = resultSet.getTimestamp("birthday");
                final Instant instant = birthday.toInstant();
                final LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
//                stu.setBirthday(localDateTime);
                stu.setClaId(resultSet.getInt("cla_id"));
                list.add(stu);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public int transaction(Connection connection, ArrayList<Student> list) {
        String sql = "insert into stu(name,age,height,sex,birthday,cla_id)values (?, ?, ?, ?, ?, ?)";
        try (final PreparedStatement preparedStatement = connection.prepareStatement(sql);) {
            Iterator<Student> iterator = list.iterator();
            int i = 0;
            while (iterator.hasNext()) {
                Student student = iterator.next();
                preparedStatement.setString(1, student.getName());
                preparedStatement.setInt(2, student.getAge());
                preparedStatement.setDouble(3, student.getHeight());
                preparedStatement.setString(4, student.getSex());
                preparedStatement.setString(5, student.getBirthday());
                preparedStatement.setInt(6, student.getClaId());
                i = preparedStatement.executeUpdate();
            }
            return i;
        } catch (SQLException throwables) {
            throw new RuntimeException("插入学习异常");
        }
    }
}
